package com.example.pandaso.engines.extend;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.example.pandaso.engines.BaseEngine;
import com.example.pandaso.utils.HttpUtils;

public class BaiduEngine extends BaseEngine {

    @Override
    public List<String> getTips(String keyword) {
        List<String> suggestsList = new ArrayList<String>();

        String response = HttpUtils.getEntity("http://suggestion.baidu.com/su?cb=&wd=" + keyword);

        int count = 0;
        
        // 提取大括号内的内容
        int startIndex = response.indexOf("{");
        int endIndex = response.lastIndexOf("}");
        String jsonText = response.substring(startIndex, endIndex-1);
        // 通过正则表达式提取属性值
        Pattern pattern = Pattern.compile("\"([^\"]+)\"");
        Matcher matcher = pattern.matcher(jsonText);
        while(matcher.find()){
            String match = matcher.group(1);
            if(count != 0){
                suggestsList.add(match);
            } 
            count++;
        }
        return suggestsList;
    }

}
